package com.sxj.corejava.code08_常用类.可变字符串;

/**
 * @author 石小俊
 * @date 2025年01月11日
 */
public class Test02_常用方法 {

    public static void main(String[] args) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("admin");
        System.out.println(buffer);
        buffer.append("Hello World");
        System.out.println(buffer);
        buffer.append(",")
                .append("I am admin")
                .append(", Hello")
                .append(" World");
        System.out.println(buffer);
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 10; i++) {
            builder.append(i);
        }
        System.out.println(builder);
        builder.insert(3, "a");
        System.out.println(builder);

        builder.delete(3, 5);    // 左闭右开
        System.out.println(builder);
        builder.deleteCharAt(5);
        System.out.println(builder);
        System.out.println(builder.length());

        // 如果设置的长度比原有字符串长度长,则在末尾添加ASCII为0的字符,一直添加到设置的长度为止
        builder.setLength(10);
        System.out.println(builder);

        // 如果设置的长度比原有字符串长度短,则直接舍去多余的字符
        builder.setLength(5);
        System.out.println(builder);

        builder = builder.reverse();
        System.out.println(builder);

        // 所有的可变字符串均可以转换成字符
        String s = builder.toString();
        System.out.println(s);

    }

}
